package mybatis.web;

import mybatis.pojo.Grade;
import mybatis.services.GradeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 汤舒青
 * @Version 1.0
 */

@WebServlet("/addGrade")
public class GradeAdd extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/jsp/gradeAdd.jsp").forward(req,resp);
       // resp.sendRedirect("/WEB-INF/jsp/gradeAdd.jsp");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String gName = request.getParameter("gname");
        PrintWriter out = response.getWriter();
        Grade grade = new Grade();
        grade.setGname(gName);
        int r = GradeService.ME.add(grade);
        if (r > 0) {
            out.println("<script>alert('增加成功');window.location.replace('grade');</script>");

            // request.getRequestDispatcher("subject.jsp").forward(request,response);
        } else {
            // out.print("增加失败，可能课程名重复了！");
            request.setAttribute("msg", "增加失败，可能课程名重复了！");
            request.getRequestDispatcher("grade").forward(request, response);
        }
    }
}